<?php
namespace Home\Controller;
use Think\Controller;
class RegisterController extends BaseController{
	
	//显示注册页面
	public function index(){
		$this->get_header();
		$this->get_topcategory();
		$this->display();	
	}
	
	//添加新用户到表
	public function insert(){
		if (empty($_POST['user_name'])) {
			$this->error("用户名不能为空!");
		}
		$data['user_name']=$_POST['user_name'];
		$data['pwd']=$_POST['pwd'];
		$verify_code=$_POST['verify'];
		if (!check_code($verify_code)){
			$this->error("验证码错误!");
		}
		$regmodel=M("User");
		//开启事务
		$regmodel->startTrans();
		$user_id=$regmodel->add($data);
		if ($user_id) {
			$datainfo['user_id']=$user_id;
			$datainfo['create_time']=time();
			$userinfomodel=M('Userinfo');
			if ($userinfomodel->add($datainfo)) {
				$regmodel->commit();
				session(null);
				session('name',$data['user_name']);
				session('id',$user_id);
				$this->success('注册成功','../Index/index');
			}else {
				$regmodel->rollback();
				$this->error("添加失败!");
			}
		}
	}
	
		//生成验证码
	public function verify(){
		$config =    array(    
				'fontSize'    =>    20,    // 验证码字体大小
				'length'      =>    4,     // 验证码位数
				'useNoise'    =>    true   	// 关闭验证码杂点
		);
		$Verify =     new \Think\Verify($config);
		return  $Verify->entry();
	}
	//获取网站信息
	private function get_header(){
		$webinfomodel=M("Webinfo");
		$webinfo=$webinfomodel->find(1);
		$this->assign("webinfo",$webinfo);
	}
	//获取导航
	private function get_topcategory() {
		$caregorymodel=M("Category");
		$top_categpry=$caregorymodel->where("parent_id=0")->select();
		$this->assign("top_categpry",$top_categpry);
	}
	

	
	
	
	
	
	
	
	
}